import { ref, onMounted } from 'vue'
import http from '@/lib/http'
import type { Product } from '../types'

export function useProducts() {
  const products = ref<Product[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchProducts(params: Record<string, any> = {}) {
    loading.value = true
    error.value = null
    try {
      const res = await http.get('/good', { params })
      const records = res?.data?.data?.records
      products.value = Array.isArray(records) ? records : []
    } catch (e: any) {
      error.value = e?.message || '加载失败'
    } finally {
      loading.value = false
    }
  }

  onMounted(fetchProducts)

  return { products, loading, error, fetchProducts }
}

